* This file should be used with the data in "Stuck_in_the_Middle_simulation_results_data.dta".
* It includes replication of Figures 2-5.


use "Stuck_in_the_Middle_simulation_results_data"
set more off
log using "Stuck_in_the_Middle_simulation_log", replace
* Labels for outliers
gen p_year = "FDP 2009" if cyear==4 & party ==1
replace p_year = "Die Linke 2009" if cyear==4 & party==4
replace p_year = "Canadian Green 2008" if cyear==1 & party==5
replace p_year = "Labour 2017" if cyear==9 & party==3

* Create dummy variables center group v each separate group
recode party (1=1) (2=0) (else=.), gen(c_right) 
recode party (1=1) (3=0) (else=.), gen(c_left)
recode party (1=1) (4=0) (else=.), gen(c_other_l)
recode party (1=1) (5=0) (else=.), gen(c_green)
recode party (1=1) (6=0) (else=.), gen(c_p_right)

* Testing differences in gains from positioning (results presented in Figure 2)
ttest gain, by(c_right)
ttest gain, by(c_left)
ttest gain, by(c_other_l)
ttest gain, by(c_green)
ttest gain, by(c_p_right)

* Figure 2: Simulated Vote Share Increase from Moving to Vote-Maximizing Position
graph box gain if party<7, over(party, sort(1))  ///
box(1, col(black))  marker(1, mcol(black) mlab(p_year) mlabcol(black) mlabposition(12)) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right))
graph save Graph "Figure_2.gph" ,replace 
graph export "Figure_2.png", as(png) replace

* Figure 3: Distance Between Parties’ Perceived and Vote-Maximizing Left-Right Position
graph box move if party<7, over(party, sort(1))  ///
box(1, col(black))  marker(1, mcol(black) mlab(p_year) mlabcol(black) mlabposition(12)) ///
ytitle("Distnace from Vote-Maximizing Position", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right))
graph save Graph "Figure_3.gph" ,replace 
graph export "Figure_3.png", as(png) replace

* Figure 4: Valence and Simulated Vote Share Increase at Vote-Maximizing Position
scatter gain valence if party==1, msymbol(O) mcol(black) || ///
scatter gain valence if party==2, msymbol(X) mcol(gray) || ///
scatter gain valence if party==4, msymbol(+) mcol(gray) || ///
scatter gain valence if party==3, msymbol(oh) mcol(gray) || ///
scatter gain valence if party==6, msymbol(d) mcol(gray) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid)  ///
xtitle("Valence Relative to the Green Party", c(black)) ///
graphregion(color(white)) bgcolor(white) legend(lab(1 "Center") lab(2 "Moderate Right") lab(3 "Other Left") lab(4 "Major Left") lab(5 "Populist-Right")) ///
title("", c(black) j(right))
graph save Graph "Figure_4.gph" ,replace 
graph export "Figure_4.png", as(png) replace

* Testing differences in gains from valence (results presented in Figure 5)
ttest gain_v, by(c_right)
ttest gain_v, by(c_left)
ttest gain_v, by(c_other_l)
ttest gain_v, by(c_green)
ttest gain_v, by(c_p_right)

*Figure 5: Vote Share Increase from One Unit Improve in Parties’ Valence Image
graph box gain_v if party<7, over(party, sort(1))  ///
box(1, col(black))  marker(1, mcol(black) mlab(p_year) mlabcol(black) mlabposition(12)) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right))
graph save Graph "Figure_5.gph" ,replace 
graph export "Figure_5.png", as(png) replace


log close
